package com.example.fullexample.service;

import com.example.fullexample.service.command.DeleteStudentCommand;
import com.example.fullexample.service.command.DeleteStudentCommandHandler;
import com.example.fullexample.service.command.DeleteStudentCompletedEvent;
import com.example.fullexample.service.domain.Student;
import com.example.fullexample.service.query.StudentQuery;
import com.example.fullexample.service.query.StudentQueryHandler;
import com.google.common.eventbus.EventBus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class StudentService implements
        DeleteStudentCommandHandler,
        StudentQueryHandler {

    @Autowired
    private EventBus bus;

    @Override
    public Student action(StudentQuery query) {
        System.out.println("先查询Redis，如果Redis中没有，再查询数据库");

        return new Student(1L, "tom", 20);
    }

    @Override
    public void action(DeleteStudentCommand command) {
        System.out.println("先删除数据库，再删除Redis");
        System.out.println("执行删除学生[" + command.getId() + "]的逻辑");

        // 广播、通知：id 为 xxx 的学生已经被删除了。
        bus.post(new DeleteStudentCompletedEvent(command.getId()));
    }
}
